package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.UserBankDao;
import com.baichang.taoshunda.entity.UserBank;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class UserBankDaoImpl extends BaseDaoImpl<UserBank> implements UserBankDao {

    /**
     * 根据商家编号获取商家的银行卡
     *
     * @param bussId 商家编号
     * @return
     */
    @Override
    public List<Map<String, Object>> findBusinessBankCard(Integer bussId) {
        String sql = " SELECT bk.*,bt.card_name bankTypeName FROM {table} bk INNER JOIN t_bankcard_type bt ON bk.card_type_id=bt.id WHERE bussine_id=? AND bk.state=1 AND bk.state=1 ";
        return queryForMapList(sql, bussId);
    }

    /**
     * 修改商家银行卡信息
     *
     * @param bankId     银行卡编号
     * @param bankTypeId 银行卡编号
     * @param bankNo     银行卡号
     * @return
     */
    @Override
    public int updateBusinessBankCard(Integer bankId, Integer bankTypeId, String bankNo) {
        String sql = " UPDATE {table} SET card_type_id=?,card_number=? WHERE id=? ";
        return update(sql, bankTypeId, bankNo, bankId);
    }

    /**
     * 解绑银行卡
     *
     * @param bankId 银行卡编号
     * @return
     */
    @Override
    public int unBundling(Integer bankId) {
        String sql = " UPDATE {table} SET state=? WHERE id=? ";
        return update(sql, 0, bankId);
    }

    /**
     * 根据编号获取银行卡信息
     *
     * @param bankcardId 商家编号
     * @return
     */
    @Override
    public Map<String, Object> getUserBankById(Integer bankcardId) {
        String sql = " SELECT ub.*,bt.card_name bankTypeName FROM t_user_bank ub INNER JOIN t_bankcard_type bt ON ub.card_type_id=bt.id WHERE ub.id=? AND ub.state=1 ";
        return queryForMap(sql, bankcardId);
    }

    /**
     * 查询配送员的所有银行卡
     *
     * @param disId 配送员编号
     * @return
     */
    @Override
    public List<Map<String, Object>> findDeliveryBankCard(Integer disId) {
        String sql = "SELECT bk.*,bt.card_name bankTypeName FROM {table} bk INNER JOIN t_bankcard_type bt ON bk.card_type_id=bt.id WHERE bk.delivery_id=? and bk.state=1";
        return queryForMapList(sql, disId);
    }

    /**
     * 根据银行卡号获取绑定的银行卡信息
     *
     * @param cardNumber 银行卡号
     * @return
     */
    @Override
    public UserBank getuserBankByCardNumber(String cardNumber) {
        String sql = " SELECT * FROM {table} WHERE card_number=? AND state=1 ";
        return queryForBean(sql, cardNumber);
    }

    @Override
    public Map<String, Object> getDeliveryBankById(Integer bankcardId) {
        String sql = "SELECT bk.*,bt.card_name bankTypeName FROM {table} bk INNER JOIN t_bankcard_type bt ON bk.card_type_id=bt.id WHERE bk.id=? and bk.state=1";
        return queryForMap(sql, bankcardId);
    }

    @Override
    public List<Map<String, Object>> findUserBankCard(Integer userId) {
        String sql = "SELECT bk.*,bt.card_name bankTypeName FROM {table} bk INNER JOIN t_bankcard_type bt ON bk.card_type_id=bt.id WHERE bk.user_id=? and bk.state=1";
        return queryForMapList(sql, userId);
    }
}